home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / source / music4c.sit / Music4C Folder / SFConvert folder / sdTo.c < prev    next >
C/C++ Source or Header  |  1990-06-26  |  10KB  |  450 lines

  1. #include    "SFConvert.h"
  2. #include    <stdio.h>
  3. #include    <unix.h>
  4. #include    <string.h>
  5. #include    <math.h>
  6. #include    <SANE.h>
  7. #include    "SDtype.h"
  8.  
  9.  
  10.  
  11. extern    long            TotalSamps;
  12. extern    CursHandle        watchCurs;
  13. extern    int                SoundFileType;
  14. extern    long            SampleRate;
  15. extern    long            NumChannels;
  16. extern    double            MaxSample;
  17. extern    double            MinSample;
  18. extern    long            fileSize;
  19. extern    ioParam            myIOParmBlk;
  20. extern    ioParam            NewParmBlk;
  21. extern    int                SFOUTPUTtype;    
  22. extern    Boolean            SDnoResource;
  23. extern    OSErr            theErr;
  24. extern    Str255            theMess;
  25. extern    int                nrec;
  26. extern    long            RecLength;
  27.  
  28. Boolean    SD1ToAIFF(void);
  29. Boolean    SD2ToAIFF(void);
  30. Boolean    SD2ToFloat(void);
  31. Boolean    SD1ToFloat(void);
  32. Boolean    SD2ToFloat(void);
  33. Boolean    SD2ToCHUNKY(void);
  34. Boolean    SD1ToCHUNKY(void);
  35. Boolean    SD2ToINT16(void);
  36. Boolean    SD1ToINT16(void);
  37.  
  38.  
  39.  
  40. extern    void    OSError(Str255, Str255);
  41. extern    void    DoOSErrorAlert(Str255, Str255);
  42.  
  43. Boolean    SD1ToAIFF()
  44. {
  45.     return(FALSE);
  46. }
  47. Boolean    SD2ToAIFF()
  48. {
  49.     return(FALSE);
  50. }
  51.  
  52. Boolean    SD2ToFloat()
  53. {
  54.     register    long    i;
  55.     register    double    x;
  56.     register    int    ix;
  57.     double        peak;
  58.     Str255        mess;
  59.     int            *theIbuf, *Iptr;
  60.     float        *sp, *SampBuf;
  61.     long        nBytes;
  62.     long        nSamps;
  63.     long        bytesLeft;
  64.     long        sampBufsz;
  65.  
  66.  
  67.     myIOParmBlk.ioCompletion = NIL;
  68.     PBGetEOF(&myIOParmBlk, FALSE);
  69.     if ( (fileSize = (long)myIOParmBlk.ioMisc) <= 0 ) {
  70.         OSError("\pfile is empty!", NIL);
  71.         return(FALSE);
  72.     }
  73.     
  74.     TotalSamps =  fileSize / sizeof(int);
  75. /*    SampleRate = Header.SampRate;*/
  76. /*    get sample raste from resource */
  77.  
  78.  
  79.     nrec = 0;
  80.     SetProgressDialog();
  81.     sampBufsz  = RecLength  / sizeof(int);
  82.     theIbuf = (int *)NewPtr(sizeof(int) * sampBufsz);
  83.     if ( (theErr = MemError()) != noErr ) {
  84.         DoOSErrorAlert("\pcan't get enough memory  for theIbuf", NIL);
  85.         return(FALSE);
  86.     }    
  87.     SampBuf = (float *)NewPtr( sizeof(float) * sampBufsz);
  88.     if ( (theErr = MemError()) != noErr ) {
  89.         DoOSErrorAlert("\pcan't get enough memory  for SampBuf", NIL);
  90.         return(FALSE);
  91.     }
  92.     
  93.     
  94.     
  95.     MinSample = 0.0;
  96.     MaxSample = 0.0;
  97.     
  98.     
  99.         
  100.     bytesLeft = fileSize;
  101.  
  102.     myIOParmBlk.ioPosMode = fsAtMark;
  103.     myIOParmBlk.ioPosOffset = NIL;
  104.  
  105.     while ( bytesLeft > 0L ) {
  106.         if ( bytesLeft > RecLength )
  107.             nBytes = RecLength;
  108.         else
  109.             nBytes = bytesLeft;
  110.             
  111.         myIOParmBlk.ioBuffer = (Ptr)theIbuf;
  112.         myIOParmBlk.ioReqCount = nBytes;
  113.         theErr = PBRead(&myIOParmBlk, FALSE);
  114.         if (theErr != noErr ) {
  115.             if ( theErr == eofErr )
  116.                 DoOSErrorAlert("\pEnd of file", NIL);
  117.             return(FALSE);
  118.         }
  119.         nBytes = myIOParmBlk.ioActCount;
  120.         if ( nBytes != myIOParmBlk.ioReqCount )
  121.                 DoOSErrorAlert("\pRead less bytes than expected", NIL);
  122.         nSamps = nBytes / sizeof(int);
  123.         for ( i = 0, Iptr = theIbuf, sp = SampBuf; i < nSamps; i++ ) {
  124.             ix = *Iptr++;
  125.             x = (double)(ix);
  126.             if ( x < MinSample )
  127.                 MinSample = x;
  128.             if ( x > MaxSample )
  129.                 MaxSample = x;
  130.             *sp++ = x;
  131.         }
  132.  
  133. /* write it out */
  134.         NewParmBlk.ioReqCount = (long)(nSamps * sizeof(float));
  135.         NewParmBlk.ioBuffer = (Ptr)SampBuf;
  136.         if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) { 
  137.             DoOSErrorAlert("\pError writing to sample file", NIL);
  138.         }
  139.         if ( NewParmBlk.ioActCount != (long)(nSamps * sizeof(float)) ) {
  140.             DoOSErrorAlert("\pError writing to sample file, wrote wrong number of bytes", NIL);
  141.         }
  142.  
  143.         nrec++;
  144.         if ( !UpdateProgressDialog() ) {
  145.             DisposPtr((Ptr)SampBuf);
  146.             DisposPtr((Ptr)theIbuf);
  147.             DisposeProgDialog();
  148.             InitCursor();
  149.             return(FALSE);
  150.         }
  151.         bytesLeft -= nBytes;
  152.     }
  153.     DisposPtr((Ptr)SampBuf);    
  154.     DisposPtr((Ptr)theIbuf);    
  155.     DisposeProgDialog();
  156.     InitCursor();
  157.     return(TRUE);
  158. }
  159.  
  160.  
  161. Boolean    SD1ToFloat()
  162. {
  163.     register    long    i;
  164.     register    double    x;
  165.     register    int    ix;
  166.     double        peak;
  167.     Str255        mess;
  168.     int            *theIbuf, *Iptr;
  169.     float        *sp, *SampBuf;
  170.     long        nBytes;
  171.     long        nSamps;
  172.     long        bytesLeft;
  173.     long        sampBufsz;
  174.     WaveRec        Header;
  175.     WavPtr        HeaderPtr;
  176.     
  177.     
  178.     RecLength = (long)(16384);
  179. /* read the header */
  180.     HeaderPtr = &Header;
  181.     nBytes = sizeof(Header.HeaderSize);
  182.     myIOParmBlk.ioPosMode = fsFromStart;
  183.     myIOParmBlk.ioPosOffset = 0L;
  184.     myIOParmBlk.ioBuffer = (Ptr)HeaderPtr;
  185.     myIOParmBlk.ioReqCount = nBytes;
  186.     theErr = PBRead(&myIOParmBlk, FALSE);
  187.     if (theErr != noErr ) {
  188.         if ( theErr == eofErr )
  189.             DoOSErrorAlert("\pEnd of file reached Header", NIL);
  190.         else {
  191.             sprintf((char *)theMess, "\pError reading Header, theErr = %d\n", theErr);
  192.             DoOSErrorAlert(theMess, NIL);
  193.             return(FALSE);
  194.         }
  195.         return(FALSE);
  196.     }
  197.     if ( myIOParmBlk.ioActCount != nBytes ) {
  198.         DoOSErrorAlert("\perror reading HeaderSize", NIL);
  199.     }
  200.  
  201.     nBytes = Header.HeaderSize;
  202.     myIOParmBlk.ioPosMode = fsFromStart;
  203.     myIOParmBlk.ioPosOffset = 0L;
  204.     myIOParmBlk.ioBuffer = (Ptr)HeaderPtr;
  205.     myIOParmBlk.ioReqCount = nBytes;
  206.     theErr = PBRead(&myIOParmBlk, FALSE);
  207.     if (theErr != noErr ) {
  208.         if ( theErr == eofErr )
  209.             DoOSErrorAlert("\pEnd of file reached Header", NIL);
  210.         else {
  211.             sprintf((char *)theMess, "\pError reading Header, theErr = %d\n", theErr);
  212.             DoOSErrorAlert(theMess, NIL);
  213.         }
  214.         return(FALSE);
  215.     }
  216.     
  217.     
  218.     fileSize = Header.FileSize;
  219.     TotalSamps =  fileSize / sizeof(int);
  220.     SampleRate = Header.SampRate;
  221.     nrec = 0;
  222.     SetProgressDialog();
  223.     sampBufsz  = RecLength  / sizeof(int);
  224.     theIbuf = (int *)NewPtr(sizeof(int) * sampBufsz);
  225.     if ( (theErr = MemError()) != noErr ) {
  226.         DoOSErrorAlert("\pcan't get enough memory  for theIbuf", NIL);
  227.         return(FALSE);
  228.     }    
  229.     SampBuf = (float *)NewPtr( sizeof(float) * sampBufsz);
  230.     if ( (theErr = MemError()) != noErr ) {
  231.         DoOSErrorAlert("\pcan't get enough memory  for SampBuf", NIL);
  232.         return(FALSE);
  233.     }
  234.     
  235.     MinSample = 0.0;
  236.     MaxSample = 0.0;
  237.     
  238.         
  239.     bytesLeft = fileSize;
  240.  
  241.     myIOParmBlk.ioPosMode = fsAtMark;
  242.     myIOParmBlk.ioPosOffset = NIL;
  243.  
  244.     while ( bytesLeft > 0L ) {
  245.         if ( bytesLeft > RecLength )
  246.             nBytes = RecLength;
  247.         else
  248.             nBytes = bytesLeft;
  249.             
  250.         myIOParmBlk.ioBuffer = (Ptr)theIbuf;
  251.         myIOParmBlk.ioReqCount = nBytes;
  252.         theErr = PBRead(&myIOParmBlk, FALSE);
  253.         if (theErr != noErr ) {
  254.             if ( theErr == eofErr )
  255.                 DoOSErrorAlert("\pEnd of file", NIL);
  256.             return(FALSE);
  257.         }
  258.         nBytes = myIOParmBlk.ioActCount;
  259.         if ( nBytes != myIOParmBlk.ioReqCount )
  260.                 DoOSErrorAlert("\pRead less bytes than expected", NIL);
  261.         nSamps = nBytes / sizeof(int);
  262.         for ( i = 0, Iptr = theIbuf, sp = SampBuf; i < nSamps; i++ ) {
  263.             ix = *Iptr++;
  264.             x = (double)(ix);
  265.             if ( x < MinSample )
  266.                 MinSample = x;
  267.             if ( x > MaxSample )
  268.                 MaxSample = x;
  269.             *sp++ = x;
  270.         }
  271.  
  272. /* write it out */
  273.         NewParmBlk.ioReqCount = (long)(nSamps * sizeof(float));
  274.         NewParmBlk.ioBuffer = (Ptr)SampBuf;
  275.         if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) { 
  276.             DoOSErrorAlert("\pError writing to sample file", NIL);
  277.         }
  278.         if ( NewParmBlk.ioActCount != (long)(nSamps * sizeof(float)) ) {
  279.             DoOSErrorAlert("\pError writing to sample file, wrote wrong number of bytes", NIL);
  280.         }
  281.  
  282.         nrec++;
  283.         if ( !UpdateProgressDialog() ) {
  284.             DisposPtr((Ptr)SampBuf);
  285.             DisposPtr((Ptr)theIbuf);
  286.             DisposeProgDialog();
  287.             InitCursor();
  288.             return(FALSE);
  289.         }
  290.         bytesLeft -= nBytes;
  291.     }
  292.     DisposPtr((Ptr)SampBuf);    
  293.     DisposPtr((Ptr)theIbuf);    
  294.     DisposeProgDialog();
  295.     InitCursor();
  296.     return(TRUE);
  297. }
  298.  
  299. Boolean    SD2ToCHUNKY()
  300. {
  301.     return(FALSE);
  302. }
  303.  
  304. Boolean    SD1ToCHUNKY()
  305. {
  306. }
  307.  
  308. Boolean    SD2ToINT16()
  309. {
  310.     return(FALSE);
  311. }
  312.  
  313. Boolean    SD1ToINT16()
  314. {
  315.     register    long    i;
  316.     register    double    scalefactor;
  317.     double        peak;
  318.     Str255        mess;
  319.     int            *theIbuf, *Iptr;
  320.     long        nBytes;
  321.     long        nSamps;
  322.     long        bytesLeft;
  323.     long        sampBufsz;
  324.     WaveRec        Header;
  325.     WavPtr        HeaderPtr;
  326.     
  327.     
  328.     RecLength = (long)(16384);
  329.     HeaderPtr = &Header;
  330. /* read the header */
  331.     nBytes = sizeof(Header.HeaderSize);
  332.     myIOParmBlk.ioPosMode = fsFromStart;
  333.     myIOParmBlk.ioPosOffset = 0L;
  334.     myIOParmBlk.ioBuffer = (Ptr)HeaderPtr;
  335.     myIOParmBlk.ioReqCount = nBytes;
  336.     theErr = PBRead(&myIOParmBlk, FALSE);
  337.     if (theErr != noErr ) {
  338.         if ( theErr == eofErr )
  339.             DoOSErrorAlert("\pEnd of file reached Header", NIL);
  340.         else {
  341.             sprintf((char *)theMess, "\pError reading Header, theErr = %d\n", theErr);
  342.             DoOSErrorAlert(theMess, NIL);
  343.         }
  344.         return(FALSE);
  345.     }
  346.     if ( myIOParmBlk.ioActCount != nBytes ) {
  347.         DoOSErrorAlert("\perror reading HeaderSize", NIL);
  348.     }
  349.  
  350.     nBytes = Header.HeaderSize;
  351.     myIOParmBlk.ioPosMode = fsFromStart;
  352.     myIOParmBlk.ioPosOffset = 0L;
  353.     myIOParmBlk.ioBuffer = (Ptr)HeaderPtr;
  354.     myIOParmBlk.ioReqCount = nBytes;
  355.     theErr = PBRead(&myIOParmBlk, FALSE);
  356.     if (theErr != noErr ) {
  357.         if ( theErr == eofErr )
  358.             DoOSErrorAlert("\pEnd of file reached Header", NIL);
  359.         else {
  360.             sprintf((char *)theMess, "\pError reading Header, theErr = %d\n", theErr);
  361.             DoOSErrorAlert(theMess, NIL);
  362.             return(FALSE);
  363.         }
  364.         return(FALSE);
  365.     }
  366.     
  367.     fileSize = Header.FileSize;
  368.     TotalSamps =  fileSize / sizeof(int);
  369.     SampleRate = Header.SampRate;
  370.     nrec = 0;
  371.     SetProgressDialog();
  372.     
  373.     sampBufsz  = RecLength  / sizeof(int);
  374.     theIbuf = (int *)NewPtr(sizeof(int) * sampBufsz);
  375.     if ( (theErr = MemError()) != noErr ) {
  376.         DoOSErrorAlert("\pcan't get enough memory  for theIbuf", NIL);
  377.         return(FALSE);
  378.     }    
  379.     
  380.     if ( SDnoResource ) {
  381.         scalefactor = 1.0;
  382.         NumChannels = 1;
  383.         MinSample = 0.0;
  384.         MaxSample = 0.0;
  385.     }
  386.     else {
  387.         peak = MAX(fabs(MinSample), fabs(MaxSample));
  388.         scalefactor = (SAMPMAX / 2.0) / peak;
  389.         MinSample = 0.0;
  390.         MaxSample = 0.0;
  391.     }
  392.     
  393.     bytesLeft = fileSize;
  394.  
  395.     myIOParmBlk.ioPosMode = fsAtMark;
  396.     myIOParmBlk.ioPosOffset = NIL;
  397.  
  398.     while ( bytesLeft > 0L ) {
  399.         if ( bytesLeft > RecLength )
  400.             nBytes = RecLength;
  401.         else
  402.             nBytes = bytesLeft;
  403.             
  404.         myIOParmBlk.ioBuffer = (Ptr)theIbuf;
  405.         myIOParmBlk.ioReqCount = nBytes;
  406.         theErr = PBRead(&myIOParmBlk, FALSE);
  407.         if (theErr != noErr ) {
  408.             if ( theErr == eofErr )
  409.                 DoOSErrorAlert("\pEnd of file reached in read form MACread_set", NIL);
  410.             return(FALSE);
  411.         }
  412.         nBytes = myIOParmBlk.ioActCount;
  413.         if ( nBytes != myIOParmBlk.ioReqCount )
  414.                 DoOSErrorAlert("\pRead less bytes than expected", NIL);
  415.         nSamps = nBytes / sizeof(int);
  416.         for ( i = 0, Iptr = theIbuf; i < nSamps; i++ ) {
  417.             *Iptr *= scalefactor;
  418.             if ( *Iptr < MinSample )
  419.                 MinSample = *Iptr;
  420.             if ( *Iptr > MaxSample )
  421.                 MaxSample = *Iptr;
  422.             *Iptr++;
  423.         }
  424.  
  425.  
  426. /* write it out */
  427.         NewParmBlk.ioReqCount = (long)(nSamps * sizeof(int));
  428.         NewParmBlk.ioBuffer = (Ptr)theIbuf;
  429.         if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) { 
  430.             DoOSErrorAlert("\pError writing to sample file", NIL);
  431.         }
  432.         if ( NewParmBlk.ioActCount != (long)(nSamps * sizeof(int)) ) {
  433.             DoOSErrorAlert("\pError writing to sample file, wrote wrong number of bytes", NIL);
  434.         }
  435.  
  436.         nrec++;
  437.         if ( !UpdateProgressDialog() ) {
  438.             DisposPtr((Ptr)theIbuf);
  439.             DisposeProgDialog();
  440.             InitCursor();
  441.             return(FALSE);
  442.         }
  443.         bytesLeft -= nBytes;
  444.     }
  445.     DisposPtr((Ptr)theIbuf);    
  446.     DisposeProgDialog();
  447.     InitCursor();
  448.     return(TRUE);
  449. }
  450.